package com.example.yichuangyun.mvvm.ui.fragment.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.GridLayoutManager;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.FragmentOfficialInfoBinding;
import com.example.yichuangyun.model.EventBusModel;
import com.example.yichuangyun.model.company.CompanyDataModel;
import com.example.yichuangyun.model.company.CompanyDetailsContactsDataModel;
import com.example.yichuangyun.model.company.CompanyLabelModel;
import com.example.yichuangyun.model.company.GsBaseInfoModel;
import com.example.yichuangyun.model.company.GsChangeRecordDataModel;
import com.example.yichuangyun.model.company.IntellectualPropertyDataModel;
import com.example.yichuangyun.model.company.ManageInfoDataModel;
import com.example.yichuangyun.model.company.OutsideInvestDataModel;
import com.example.yichuangyun.model.company.RiskInfoDataModel;
import com.example.yichuangyun.mvvm.base.BaseViewModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseModel;
import com.example.yichuangyun.mvvm.di.retrofit.BaseNetCallback;
import com.example.yichuangyun.mvvm.ui.activity.GsInfoActivity;
import com.example.yichuangyun.mvvm.ui.activity.IntellectualPropertyActivity;
import com.example.yichuangyun.mvvm.ui.activity.ManageInfoActivity;
import com.example.yichuangyun.mvvm.ui.activity.OutsideInvestActivity;
import com.example.yichuangyun.mvvm.ui.activity.RiskInfoActivity;
import com.example.yichuangyun.mvvm.ui.adapter.CompanyLabelAdapter;
import com.example.yichuangyun.utils.JumpIntent;
import com.example.yichuangyun.utils.ToastHelper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressLint("NotifyDataSetChanged")
public class OfficialInfoViewModel extends BaseViewModel {

    public MutableLiveData<CompanyDataModel.CompanyModel> companyModelMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<CompanyDataModel.CompanyModel> companyDataModelMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<CompanyDetailsContactsDataModel> contactsDataModelMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> collectSuccessMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<String> cancelCollectSuccessMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<List<CompanyDetailsContactsDataModel.ContactsModel>> contactsMutableLiveData = new MutableLiveData<>();

    private final List<CompanyLabelModel> jbxxData = new ArrayList<>();
    private final List<CompanyLabelModel> fxxxData = new ArrayList<>();
    private final List<CompanyLabelModel> jyxxData = new ArrayList<>();
    private final List<CompanyLabelModel> cqzsData = new ArrayList<>();
    private final List<CompanyLabelModel> dwtzData = new ArrayList<>();
    private CompanyLabelAdapter jbxxAdapter;
    private CompanyLabelAdapter fxxxAdapter;
    private CompanyLabelAdapter jyxxAdapter;
    private CompanyLabelAdapter cqzsAdapter;
    private CompanyLabelAdapter dwtzAdapter;

    public void initLabelData(Context mContext, FragmentOfficialInfoBinding binding, String eid) {
        jbxxData.add(new CompanyLabelModel(mContext.getString(R.string.gsxx), R.mipmap.icon_gsxx, R.mipmap.icon_gsxx_sel));
        jbxxData.add(new CompanyLabelModel(mContext.getString(R.string.gdxx), R.mipmap.icon_gdxx, R.mipmap.icon_gdxx_sel));
        jbxxData.add(new CompanyLabelModel(mContext.getString(R.string.zyry), R.mipmap.icon_zyry, R.mipmap.icon_zyry_sel));
        jbxxData.add(new CompanyLabelModel(mContext.getString(R.string.gsbg), R.mipmap.icon_gsbg, R.mipmap.icon_gsbg_sel));
        jbxxData.add(new CompanyLabelModel(mContext.getString(R.string.qynb), R.mipmap.icon_qynb, R.mipmap.icon_qynb_sel));
        jbxxAdapter = new CompanyLabelAdapter(mContext, jbxxData);
        binding.jbxxRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        binding.jbxxRecyclerView.setAdapter(jbxxAdapter);
        jbxxAdapter.setOnLabelItemClickListener((position, model) -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.positionKey, position);
            bundle.putString(Constant.eidKey, eid);
            JumpIntent.jump((Activity) mContext, GsInfoActivity.class, bundle);
        });

        fxxxData.add(new CompanyLabelModel(mContext.getString(R.string.jyyc), R.mipmap.icon_jyyc, R.mipmap.icon_jyyc_sel));
        fxxxData.add(new CompanyLabelModel(mContext.getString(R.string.xzcf), R.mipmap.icon_xzcf, R.mipmap.icon_xzcf_sel));
        fxxxData.add(new CompanyLabelModel(mContext.getString(R.string.yzwfsx), R.mipmap.icon_yzwfsx, R.mipmap.icon_yzwfsx_sel));
        fxxxData.add(new CompanyLabelModel(mContext.getString(R.string.swyc), R.mipmap.icon_swyc, R.mipmap.icon_swyc_sel));
        fxxxAdapter = new CompanyLabelAdapter(mContext, fxxxData);
        binding.fxxxRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        binding.fxxxRecyclerView.setAdapter(fxxxAdapter);
        fxxxAdapter.setOnLabelItemClickListener((position, model) -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.positionKey, position);
            bundle.putString(Constant.eidKey, eid);
            JumpIntent.jump((Activity) mContext, RiskInfoActivity.class, bundle);
        });


        jyxxData.add(new CompanyLabelModel(mContext.getString(R.string.swpj), R.mipmap.icon_swpj, R.mipmap.icon_swpj_sel));
        jyxxData.add(new CompanyLabelModel(mContext.getString(R.string.zzzs), R.mipmap.icon_zzzs, R.mipmap.icon_zzzs_sel));
        jyxxData.add(new CompanyLabelModel(mContext.getString(R.string.xzxk), R.mipmap.icon_xzxk, R.mipmap.icon_xzxk_sel));
        jyxxData.add(new CompanyLabelModel(mContext.getString(R.string.ybnsr), R.mipmap.icon_ybnsr, R.mipmap.icon_ybnsr_sel));
        jyxxAdapter = new CompanyLabelAdapter(mContext, jyxxData);
        binding.jyxxRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        binding.jyxxRecyclerView.setAdapter(jyxxAdapter);
        jyxxAdapter.setOnLabelItemClickListener((position, model) -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.positionKey, position);
            bundle.putString(Constant.eidKey, eid);
            JumpIntent.jump((Activity) mContext, ManageInfoActivity.class, bundle);
        });

        cqzsData.add(new CompanyLabelModel(mContext.getString(R.string.sbxx), R.mipmap.icon_sbxx, R.mipmap.icon_sbxx_sel));
        cqzsData.add(new CompanyLabelModel(mContext.getString(R.string.zpzzq), R.mipmap.icon_zpzzq, R.mipmap.icon_zpzzq_sel));
        cqzsData.add(new CompanyLabelModel(mContext.getString(R.string.rjzzq), R.mipmap.icon_rjzzq, R.mipmap.icon_rjzzq_sel));
        cqzsData.add(new CompanyLabelModel(mContext.getString(R.string.zl), R.mipmap.icon_zl, R.mipmap.icon_zl_sel));
        cqzsAdapter = new CompanyLabelAdapter(mContext, cqzsData);
        binding.cqzsRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        binding.cqzsRecyclerView.setAdapter(cqzsAdapter);
        cqzsAdapter.setOnLabelItemClickListener((position, model) -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.positionKey, position);
            bundle.putString(Constant.eidKey, eid);
            JumpIntent.jump((Activity) mContext, IntellectualPropertyActivity.class, bundle);
//            JumpIntent.jump((Activity) mContext, position == 3 ? PatentActivity.class : IntellectualPropertyActivity.class, bundle);
        });

        dwtzData.add(new CompanyLabelModel(mContext.getString(R.string.dwtz), R.mipmap.icon_dwtz, R.mipmap.icon_dwtz_sel));
        dwtzAdapter = new CompanyLabelAdapter(mContext, dwtzData);
        binding.dwtzRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 3));
        binding.dwtzRecyclerView.setAdapter(dwtzAdapter);
        dwtzAdapter.setOnLabelItemClickListener((position, model) -> {
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.positionKey, position);
            bundle.putString(Constant.eidKey, eid);
            JumpIntent.jump((Activity) mContext, OutsideInvestActivity.class, bundle);
        });
    }

    /**
     * 获取企业的eid
     */
    public void getEid(String creditNo) {
        Map<String, Object> map = new HashMap<>();
        map.put("creditNo", creditNo);
        addSubscribe(mRetrofitHelper.getEid(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<CompanyDataModel.CompanyModel>() {

            @Override
            public void onSuccess(BaseModel<CompanyDataModel.CompanyModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        companyModelMutableLiveData.setValue(baseModel.getData());
                    } else {
                        mErrorData.setValue("暂无企业信息");
                    }
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                mErrorData.setValue(message);
            }
        }));
    }

    /**
     * 官方基本信息
     */
    public void getOfficialInfoDetails(String eid) {
        addSubscribe(mRetrofitHelper.getOfficialBaseInfoDetail(eid).subscribeWith(new BaseNetCallback<CompanyDataModel.CompanyModel>() {

            @Override
            public void onSuccess(BaseModel<CompanyDataModel.CompanyModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        companyDataModelMutableLiveData.setValue(baseModel.getData());
                    }
                } else {
                    mErrorData.setValue(baseModel.getMsg());
                }
            }
        }));
    }

    /**
     * 企业详情联系方式
     */
    public void getContactWay(String eid) {
        addSubscribe(mRetrofitHelper.getDetailsContacts(eid).subscribeWith(new BaseNetCallback<CompanyDetailsContactsDataModel>() {

            @Override
            public void onSuccess(BaseModel<CompanyDetailsContactsDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        contactsDataModelMutableLiveData.setValue(baseModel.getData());
                    }
                }
            }
        }));
    }

    /**
     * 详情页工商信息基本信息
     */
    public void getGsBaseInfo(String eid) {
        addSubscribe(mRetrofitHelper.getGsBaseInfo(eid).subscribeWith(new BaseNetCallback<GsBaseInfoModel>() {

            @Override
            public void onSuccess(BaseModel<GsBaseInfoModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        GsBaseInfoModel model = baseModel.getData();
                        // 股东信息
                        if (null != model.getPartnersAlls() && model.getPartnersAlls().size() > 0) {
                            jbxxData.get(1).setCount(model.getPartnersAlls().size());
                        }
                        // 主要人员
                        if (null != model.getEmployeesAlls() && model.getEmployeesAlls().size() > 0) {
                            jbxxData.get(2).setCount(model.getEmployeesAlls().size());
                        }
                        // 企业年报
                        if (null != model.getReportDetails() && model.getReportDetails().size() > 0) {
                            jbxxData.get(4).setCount(model.getReportDetails().size());
                        }
                        jbxxAdapter.notifyDataSetChanged();
                    }
                }
            }
        }));
    }

    /**
     * 工商变更记录
     */
    public void getGsChangeRecord(String eid) {
        addSubscribe(mRetrofitHelper.getGsChangeRecord(eid).subscribeWith(new BaseNetCallback<GsChangeRecordDataModel>() {

            @Override
            public void onSuccess(BaseModel<GsChangeRecordDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        jbxxData.get(3).setCount(baseModel.getData().getTotal());
                        jbxxAdapter.notifyDataSetChanged();
                    }
                }
            }
        }));
    }

    /**
     * 风险信息
     */
    public void getRiskInfo(String eid) {
        addSubscribe(mRetrofitHelper.getRiskInfo(eid).subscribeWith(new BaseNetCallback<RiskInfoDataModel>() {

            @Override
            public void onSuccess(BaseModel<RiskInfoDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        RiskInfoDataModel model = baseModel.getData();
                        // 经营异常
                        if (null != model.getTabnormalList() && model.getTabnormalList().size() > 0) {
                            fxxxData.get(0).setCount(model.getTabnormalList().size());
                        }
                        // 行政处罚
                        if (null != model.getTadministrativePunishments() && model.getTadministrativePunishments().size() > 0) {
                            fxxxData.get(1).setCount(model.getTadministrativePunishments().size());
                        }
                        // 严重违法失信
                        if (null != model.getTseriousIllegals() && model.getTseriousIllegals().size() > 0) {
                            fxxxData.get(2).setCount(model.getTseriousIllegals().size());
                        }
                        // 税务异常-非正常户表 + 税务异常-重大税收违法
                        int count = 0;
                        if (null != model.getTabnormalEnterprises() && model.getTabnormalEnterprises().size() > 0) {
                            count += model.getTabnormalEnterprises().size();
                        }
                        if (null != model.getThugeTaxPunishments() && model.getThugeTaxPunishments().size() > 0) {
                            count += model.getThugeTaxPunishments().size();
                        }
                        fxxxData.get(3).setCount(count);
                        fxxxAdapter.notifyDataSetChanged();
                    }
                }
            }
        }));
    }

    /**
     * 经营信息
     */
    public void getManageInfo(String eid) {
        addSubscribe(mRetrofitHelper.getManageInfo(eid).subscribeWith(new BaseNetCallback<ManageInfoDataModel>() {

            @Override
            public void onSuccess(BaseModel<ManageInfoDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        ManageInfoDataModel model = baseModel.getData();
                        if (null != model.getPayTaxes() && model.getPayTaxes().size() > 0) {
                            jyxxData.get(0).setCount(model.getPayTaxes().size());
                        }
                        if (null != model.getCertificates() && model.getCertificates().size() > 0) {
                            jyxxData.get(1).setCount(model.getCertificates().size());
                        }
                        if (null != model.getLicenseInfos() && model.getLicenseInfos().size() > 0) {
                            jyxxData.get(2).setCount(model.getLicenseInfos().size());
                        }
                        if (null != model.getGeneralTaxpayers() && model.getGeneralTaxpayers().size() > 0) {
                            jyxxData.get(3).setCount(model.getGeneralTaxpayers().size());
                        }

                        jyxxAdapter.notifyDataSetChanged();
                    }
                }
            }
        }));
    }

    /**
     * 知识产权
     */
    public void getIntellectualProperty(String eid) {
        addSubscribe(mRetrofitHelper.getIntellectualProperty(eid).subscribeWith(new BaseNetCallback<IntellectualPropertyDataModel>() {

            @Override
            public void onSuccess(BaseModel<IntellectualPropertyDataModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        IntellectualPropertyDataModel model = baseModel.getData();
                        if (null != model.getTtrademarkInfos() && model.getTtrademarkInfos().size() > 0) {
                            cqzsData.get(0).setCount(model.getTtrademarkInfos().size());
                        }
                        if (null != model.getCopyrightsRelationsList() && model.getCopyrightsRelationsList().size() > 0) {
                            cqzsData.get(1).setCount(model.getCopyrightsRelationsList().size());
                        }
                        if (null != model.getCopyrightsRelations() && model.getCopyrightsRelations().size() > 0) {
                            cqzsData.get(2).setCount(model.getCopyrightsRelations().size());
                        }
                        if (null != model.getPatentsRelationsNews() && model.getPatentsRelationsNews().size() > 0) {
                            cqzsData.get(3).setCount(model.getPatentsRelationsNews().size());
                        }
                        cqzsAdapter.notifyDataSetChanged();
                    }
                }
            }
        }));
    }

    /**
     * 对外投资
     */
    public void getInvestment(String eid) {
        addSubscribe(mRetrofitHelper.getInvestment(eid).subscribeWith(new BaseNetCallback<List<OutsideInvestDataModel.OutsideInvestModel>>() {

            @Override
            public void onSuccess(BaseModel<List<OutsideInvestDataModel.OutsideInvestModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        List<OutsideInvestDataModel.OutsideInvestModel> list = baseModel.getData();
                        if (null != list && list.size() > 0) {
                            dwtzData.get(0).setCount(list.size());
                        }
                        dwtzAdapter.notifyDataSetChanged();
                    }
                }
            }
        }));
    }


    /**
     * 收藏企业
     */
    public void collectCompany(CompanyDataModel.CompanyModel companyModel) {
        JsonArray jsonArray = new JsonArray();
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("companyName", companyModel.getName());
        jsonObject.addProperty("creditNo", companyModel.getCreditNo());
        jsonObject.addProperty("eid", companyModel.getEid());
        jsonArray.add(jsonObject);
        Map<String, Object> map = new HashMap<>();
        map.put("collectSaves", jsonArray);
        addSubscribe(mRetrofitHelper.collectCompany(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    collectSuccessMutableLiveData.setValue(baseModel.getData());
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                ToastHelper.show(message);
            }
        }));
    }

    /**
     * 取消收藏企业
     */
    public void cancelCollectCompany(String eid) {
        addSubscribe(mRetrofitHelper.cancelCollectCompany(eid).subscribeWith(new BaseNetCallback<String>() {

            @Override
            public void onSuccess(BaseModel<String> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    cancelCollectSuccessMutableLiveData.setValue(baseModel.getData());
                    EventBus.getDefault().post(new EventBusModel(Constant.refreshCollectList));
                } else {
                    ToastHelper.show(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                ToastHelper.show(message);
            }
        }));
    }

    /**
     * 获取可拨打电话
     */
    public void getContacts(String eid) {
        addSubscribe(mRetrofitHelper.getContacts(eid).subscribeWith(new BaseNetCallback<List<CompanyDetailsContactsDataModel.ContactsModel>>() {

            @Override
            public void onSuccess(BaseModel<List<CompanyDetailsContactsDataModel.ContactsModel>> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    contactsMutableLiveData.setValue(baseModel.getData());
                }
            }
        }));
    }
}
